// src/user/user.controller.ts
import { Controller, Post, Body, BadRequestException } from '@nestjs/common';
import { UserService } from './user.service';
import { WechatRegisterDto } from './dto/wechat-register.dto';
import { PhoneRegisterDto } from './dto/phone-register.dto';
import { LoginDto } from './dto/login.dto';
import { RemoveDto } from './dto/remove.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { Public } from '../auth/public.decorator';

@Controller('user')
export class UserController {
  constructor(private readonly userService: UserService) { }

  @Public() // 标记为公共路由
  @Post('wechat-register')
  async wechatRegister(@Body() registerDto: WechatRegisterDto) {
    return this.userService.wechatRegister(registerDto);
  }

  @Public() // 标记为公共路由
  @Post('phone-register')
  async phoneRegister(@Body() registerDto: PhoneRegisterDto) {
    return this.userService.phoneRegister(registerDto);
  }

  @Public() // 标记为公共路由
  @Post('login')
  async login(@Body() loginDto: LoginDto) {
    return this.userService.login(loginDto);
  }

  @Post('updateById')
  async updateUserById(@Body() updateUserDto: UpdateUserDto) {
    try {
      return await this.userService.updateUserById(updateUserDto);
    } catch (error) {
      if (error instanceof BadRequestException) {
        return {
          isUpdated: false,
          message: error.message,
          code: error.getStatus(),
        };
      }
      throw error;
    }
  }

  @Post('remove')
  async remove(@Body() removeDto: RemoveDto) {
    try {
      return await this.userService.remove(removeDto);
    } catch (error) {
      if (error instanceof BadRequestException) {
        return {
          isUpdated: false,
          message: error.message,
          code: error.getStatus(),
        };
      }
      throw error;
    }
  }
}